package com.woniu.advice;

import com.woniu.entity.Result;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一的异常处理
 * @author tarnett
 */
@Slf4j
@Hidden
@RestControllerAdvice
public class HandleExceptionAdvice {

    @ExceptionHandler(Exception.class)
    public Result exceptionResole(Exception exception){
        log.debug("系统出bug了，exception ===== " + exception.getMessage());
        // 打印异常，方面错误排查
        exception.printStackTrace();
        return Result.error("系统繁忙，请稍后再试");
    }

    /**
     * hibernate参数校验异常统一处理
     * @param exception
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result resolveMethodArgumentNotValidException(MethodArgumentNotValidException exception){
        exception.printStackTrace();
        log.debug("系统抛出了异常，异常为：{}", exception.getMessage());
        return Result.error(exception.getBindingResult().getFieldError().getDefaultMessage());
    }

}